package sqp.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sqp.demo.dao.StudentRepository;
import sqp.demo.model.Course;
import sqp.demo.model.Student;
import sqp.demo.service.CourseService;
import sqp.demo.service.StudService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private StudService studService;
    @Autowired
    private CourseService courseService;

    @RequestMapping("/index")
    @ResponseBody
    public String hello(){
        Student student = new Student();
        student.setName("okk");
        student.setAge(8);

        List<Course> courses = courseService.findAll();
        student.setCourseList(courses);
        studService.save(student);

        return "hello"+ student.getId();
    }
    @RequestMapping("/all")
    public void hello(HttpServletResponse response){
        try {
            PrintWriter pw = response.getWriter();
            List<Student> list = studService.findAll();
            for (int i=0;i<list.size();i++){
                Student student = list.get(i);
                String names = "";
                if (student.getCourseList()!=null && !student.getCourseList().isEmpty()){
                    for (Course c:student.getCourseList()){
                        String name = c.getName();
                        if (names.isEmpty()){
                            name+=",";
                        }
                        names+=name;
                    }
                }
                pw.print("<div style='height:25px'>"+i+"  student name: "+student.getName()+"  student age: "+student.getAge()+"  course: "+names+"</div>");
            }
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/one/{id}")
    public void findOne(HttpServletResponse response,@PathVariable Long id){
        try {
            PrintWriter pw = response.getWriter();
            Student student = studService.findOne(id);
            pw.print("<div style='height:25px'> student name: " + student.getName() + "  student age: " + student.getAge() + "</div>");
            pw.flush();
            studService.testImpl();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/course/add/{name}")
    @ResponseBody
    public String addCourse(@PathVariable String name){
        Course course = new Course();
        course.setName(name);
        course.setPoints(5);
        course.setTeacher("Sun qi peng");
        courseService.save(course);
        return "add course id:"+course.getId();
    }


}
